<?php

namespace app\health\model;

use app\health\model\tracking\HealthTrackingUserRemain;
use app\health\service\ConfigService;
use think\Model;
use think\model\relation\HasOne;

class HealthUser extends Model
{
    // 表名
    protected $name = 'health_user';
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'datetime';

    // 定义时间戳字段名
    protected $createTime = 'created_at';
    protected $updateTime = false;

    /**
     * 获取用户默认头像
     * @param [type] $value
     */
    public function getAvatarAttr($value): string
    {
        return cdnurl($value ?: ConfigService::getUserConfig('avatar'), true);
    }

    /**
     * 用户渠道信息
     * @Author 张旭鹏
     * @DateTime 2023-07-12
     */
    public function userChannel(): HasOne
    {
        return $this->hasOne(HealthUserChannel::class, 'user_id');
    }

    /**
     * 关联活跃信息
     * @return HasOne
     */
    public function userRemain(): HasOne
    {
        return $this->hasOne(HealthTrackingUserRemain::class, 'user_id')->order('created_at', 'desc');
    }
}
